#include <stdio.h>
#include <stdbool.h>

#define     SIZE    5

bool IsPopOrder(const int *pPush, const int *Pop, int nlength);

int main()
{

    int push[SIZE], pop[SIZE];
    int i;
    bool result = false;

    printf("Please input the push list:\n");
    for (i = 0; i < SIZE; i++)
        scanf("%d", &push[i]);

    printf("Please input the pop list:\n");
    for (i = 0; i < SIZE; i++)
        scanf("%d", &pop[i]);

    result = IsPopOrder(push, pop, 5);

    if (result)
        printf("The pop is legal!\n");
    else
        printf("The pop is illegal!\n");
    return 0;
}


bool IsPopOrder(const int *pPush, const int *Pop, int nlength)
{
    if (pPush == NULL || Pop == NULL || nlength < 1)
        return 0;

    int push[nlength] ;
    int push_heap, pop_heap, i;

    push_heap = 0;
    pop_heap = 0;
    i = -1;

    push[++i] = pPush[push_heap++];


    while(pop_heap < nlength) {
        if (i >= 0 && Pop[pop_heap] == push[i]) {
            pop_heap++;
            i--;
        } else if (push_heap < nlength) {
            push[++i] = pPush[push_heap++];

        }else {
            break;
        }
    }

    if (pop_heap == nlength )
        return true;
    else
        return false;
}
